package GUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import MessagePacks.MessagePack;
import MessagePacks.UpdatePack;
import MessagePacks.checkTimeException;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class ExecutionLeaderScreen extends MasterGUI {

	private static final long serialVersionUID = 1L;
	private MainGUI mainGui;
	private JButton btnTimeExtentionRequest;
	private JButton btnConfirmCompletion;
	private JButton btnPhaseTime;
	private JButton btnAccept;
	private Boolean canContinue = false;
	private JTextField timeToPhase;
	private Boolean timeAdded = false;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel;
	private JTextField textField;
	private JTextField textField_1;
	private UpdatePack d;
	private UpdatePack waitStat;
	private UpdatePack updatePhs;
	private UpdatePack time;
	private UpdatePack updRes;
	private UpdatePack upExecflag;
	private checkTimeException ChkTimeExcPck;
	
	
	public ExecutionLeaderScreen(MainGUI mainGui ) {
		super(mainGui);
		this.mainGui=mainGui;
		
		panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.setBounds(0, 40, 176, 201);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		panel_2 = new JPanel();
		panel_2.setBounds(186, 40, 405, 241);
		getContentPane().add(panel_2);
		panel_2.setLayout(null);
		
		JLabel lblEnterPhaseTime = new JLabel("Enter Phase Time : ");
		lblEnterPhaseTime.setHorizontalAlignment(SwingConstants.LEFT);
		lblEnterPhaseTime.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblEnterPhaseTime.setBounds(57, 153, 147, 23);
		panel_2.add(lblEnterPhaseTime);
		
		timeToPhase = new JTextField("Enter time");
		timeToPhase.setBounds(240, 156, 126, 20);
		panel_2.add(timeToPhase);
		panel_2.setVisible(false);
		

		
		setBtnConfirmCompletion();
		setBtnTimeExtentionRequest();
		setBtnPhaseTime();
		setBtnAccept();

		
		panel = new JPanel();
		panel.setBounds(0, 0, 884, 42);
		getContentPane().add(panel);
		
		JLabel lblExecutionLeaderMenu = new JLabel("Execution Leader Menu");
		lblExecutionLeaderMenu.setFont(new Font("Dialog", Font.BOLD, 16));
		panel.add(lblExecutionLeaderMenu);
		
		timeToPhase.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Process ID : ");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblNewLabel.setBounds(57, 81, 126, 23);
		panel_2.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(240, 84, 126, 20);
		panel_2.add(textField);
		textField.setColumns(10);
		
		JLabel lblProcessPhase = new JLabel("Process phase :");
		lblProcessPhase.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblProcessPhase.setBounds(57, 115, 126, 27);
		panel_2.add(lblProcessPhase);
		
		textField_1 = new JTextField();
		textField_1.setBounds(240, 120, 126, 20);
		panel_2.add(textField_1);
		textField_1.setColumns(10);
		
	}

	public JButton getBtnConfirmCompletion() {
		return btnConfirmCompletion;
	}


	public void setBtnConfirmCompletion() {
		btnConfirmCompletion = new JButton("Confirm Completion");
		btnConfirmCompletion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				canContinue = true;
				ChkTimeExcPck = new checkTimeException(mainGui.process.getprocessID());
				mainGui.client.handleMessageFromClientUI(ChkTimeExcPck);
				updatePhase4();
				updateTimeForLevel4();
				updateRes3(0);
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Phase 3 completed.");
			}
		});
		btnConfirmCompletion.setBounds(10, 79, 156, 23);
		panel_1.add(btnConfirmCompletion);
	}


	public JButton getBtnTimeExtentionRequest() {
		return btnTimeExtentionRequest;
	}


	public void setBtnTimeExtentionRequest() {
		btnTimeExtentionRequest = new JButton("Time Extention Request");
		btnTimeExtentionRequest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
				TimeExtensionRequest TE = new TimeExtensionRequest(mainGui);
				TE.setVisible(true);
			}
		});
		btnTimeExtentionRequest.setBounds(10, 45, 156, 23);
		panel_1.add(btnTimeExtentionRequest);
	}

	public JButton getBtnPhaseTime() {
		return btnPhaseTime;
	}

	public void setBtnPhaseTime() {
		btnPhaseTime = new JButton("Phase Time");
		if(mainGui.process.getCurrentPhase()==3 && mainGui.process.getResponsableOK()==0){
			btnPhaseTime.setEnabled(true);
			btnConfirmCompletion.setEnabled(false);
			btnTimeExtentionRequest.setEnabled(false);
			}
		
		else{
			btnPhaseTime.setEnabled(false);
			}
		btnPhaseTime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textField.setText(Integer.toString(mainGui.process.getprocessID()));
				textField.setEditable(false);
				textField_1.setText(Integer.toString(mainGui.process.getCurrentPhase()));
				textField_1.setEditable(false);
				panel_2.setVisible(true);
				btnPhaseTime.setEnabled(false);
			}
		});
		btnPhaseTime.setBounds(10, 11, 156, 23);
		panel_1.add(btnPhaseTime);
	}

	public JButton getBtnAccept() {
		return btnAccept;
	}

	public void setBtnAccept() {
		btnAccept = new JButton("Accept");
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (isInteger(timeToPhase.getText())){
					updatePhaseTime();
					updateToWaitStatus();
					updateRes3(1);
					panel_2.setVisible(false);
				}
				else
					JOptionPane.showMessageDialog(serverGUI.mainframe,"Please enter time.");
			}
		});
		btnAccept.setBounds(261, 199, 89, 23);
		panel_2.add(btnAccept);
	}
	
	public void updateToWaitStatus(){
		waitStat = new UpdatePack(MessagePack.updateStatus);
		waitStat.setStatus("Waiting For Time Approval");
		waitStat.setProcId(mainGui.process.getprocessID());
		mainGui.client.handleMessageFromClientUI(waitStat);
	}
	
	public void updatePhaseTime(){
		int days = Integer.parseInt(timeToPhase.getText());
		d= new UpdatePack(MessagePack.updateleveltime);
		d.setTime(days);
		d.setProcId(mainGui.process.getprocessID());
		mainGui.client.handleMessageFromClientUI(d);
	}
	
	/**
	 * 
	 * @param s - String for checking
	 * @return Boolean
	 */
	public static boolean isInteger(String s) {
	    try { 
	        Integer.parseInt(s); 
	    } catch(NumberFormatException e) { 
	        return false; 
	    }
	    return true;
	}
	
	
	public void updatePhase4(){
		updatePhs = new UpdatePack(MessagePack.updatePhase);
		updatePhs.setPhaseNum(4);
		updatePhs.setProcId(mainGui.process.getprocessID());
		mainGui.client.handleMessageFromClientUI(updatePhs);
		upExecflag = new UpdatePack(MessagePack.updateExecTimeFlag);
		upExecflag.setProcId(mainGui.process.getprocessID());
		upExecflag.setResponOk(0);
		mainGui.client.handleMessageFromClientUI(upExecflag);
	}
	
	
	public void updateTimeForLevel4() {
		time= new UpdatePack(MessagePack.updateleveltime);
		time.setTime(7);
		time.setPhaseNum(4);
		time.setProcId(mainGui.process.getprocessID());
		mainGui.client.handleMessageFromClientUI(time);
	}
	
	/**
	 * 
	 * @param responsible - The worker responsible for a phase
	 */
	public void updateRes3(int responsible){
		updRes= new UpdatePack(MessagePack.updresponsible);
		updRes.setProcId(mainGui.process.getprocessID());
		updRes.setResponOk(responsible);
		mainGui.client.handleMessageFromClientUI(updRes);
		
	}
}
